package com.sxkj.web.config;

import cn.hutool.core.io.resource.ClassPathResource;
import com.github.wxpay.sdk.IWXPayDomain;
import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayDomainSimpleImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.*;

import static com.github.wxpay.sdk.WXPayConstants.DOMAIN_API;
import static com.github.wxpay.sdk.WXPayConstants.DOMAIN_API2;

/**
 * @author Zhang Xingliang
 */
@Component
//@Configuration
@PropertySource(value = {"classpath:wx-pay.yml"})
@ConfigurationProperties(prefix = "wxpay")
@Data
public class WxPayConfig extends WXPayConfig {

    private byte[] certData;

    public WxPayConfig() throws Exception {
        // ClassPathResource().getFile() 无法在linux服务器读取到文件
        InputStream certStream = new ClassPathResource("weixinpay_cert.p12").getStream();
        this.certData = new byte[certStream.available()];
        certStream.read(certData, 0 ,certData.length);
        certStream.close();
    }

    //阿里巴巴短信模板-会员注册
    @Value("${registerTemplateId}")
    private String registerTemplateId;
    //阿里巴巴短信模板-商城支付
    @Value("${payTemplateId}")
    private String payTemplateId;

    @Value("${appid}")
    private String appID;

    @Value("${appsecret}")
    private String appSecret;

    //跳转前端链接
    @Value("${frontRedirect}")
    private String frontRedirect;

    // 支付发红包使用
    @Value("${mch_id}")
    private String mchID;

    @Value("${key}")
    private String key;

    @Value("${client_ip}")
    private String clientIp;

    @Value("${act_name}")
    private String actName;

    @Value("${wishing}")
    private String wishing;

    @Value("${send_name}")
    private String sendName;

    @Value("${remark}")
    private String remark;

    public InputStream getCertStream() {
        return new ByteArrayInputStream(this.certData);
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }

    public String getPrimaryDomain() {
        return DOMAIN_API;
    }

    public String getAlternateDomain() {
        return DOMAIN_API2;
    }
}
